<template>
  <div class="city-list" ref="wrapper">
    <div>
      <div class="area">
        <div class="button-title" border-bottom-1px border-top-1px>当前城市</div>
        <div class="button-list-container">
          <div class="button-wrap">
            <div class="button" border-radius>
              <div class="city-text"> {{this.city}}</div>
            </div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="button-title" border-bottom-1px border-top-1px>热门城市</div>
        <div class="button-list-container">
          <div class="button-wrap" v-for="item of hot" :key="item.id">
            <div class="button" border-radius @click="handleChangeCity(item.name)">
              <div class="city-text"> {{item.name}}</div>
            </div>
          </div>
        </div>
      </div>
      <div class="area alphabet" v-for="(item, key) of cities" :key="key" :ref="key">
        <div class="button-title" border-bottom-1px>{{key}}</div>
        <div class="button-city-container">
          <div
            class="city-name"
            border-bottom-1px
            v-for="innerItem of item"
            :key="innerItem.id"
            @click="handleChangeCity(innerItem.name)"
          >
            {{innerItem.name}}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import {mapState, mapActions} from 'vuex'
export default {
  name: 'CityList',
  props: {
    hot: {
      type: Array
    },
    cities: {
      type: Object
    },
    letter: {
      type: String
    }
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    handleChangeCity (city) {
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapActions({
      changeCity: 'changeCity'
    })
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper, {click: true})
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/mixins.styl'
  .city-list
    position absolute
    top 160px
    left 0
    bottom 0
    right 0
    overflow hidden
    .alphabet
      &:nth-last-of-type(1)
        margin-bottom 70px
    .area
      background-color #f5f5f5
      .button-title
        line-height 50px
        font-size 30px
        color:#333
        padding-left 20px
        box-sizing border-box
      .button-list-container
        padding:10px 40px 10px 0px
        background-color #fff
        overflow hidden
        .button-wrap
          float left
          width 33.33%
          text-align center
          line-height 44px
          box-sizing border-box
          padding 20px
          .button
            font-size 24px
            color #333
            padding 0 5px
            box-sizing border-box
            cursor pointer
            .city-text
              width 100%
              height 100%
              ellipsis()
      .button-city-container
        background-color #fff
        .city-name
          padding-right 50px
          line-height 70px
          text-indent 20px
          font-size 24px
          box-sizing border-box
          ellipsis()
          &:nth-last-of-type(1)
            border:none
</style>
